package cn.edu.csmzxy.rk233x.auth_system.security.handler;

/**
 * @author wumxing
 * @date 2025/10/15 9:57
 * @description
 */
import com.alibaba.fastjson.JSON;
import cn.edu.csmzxy.rk233x.auth_system.dto.ResultDTO;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
            throws IOException, ServletException {
        // 设置响应格式为JSON，返回403状态码
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        ResultDTO<?> result = ResultDTO.fail(403, "权限不足，无法访问此接口");
        response.getWriter().write(JSON.toJSONString(result));
    }
}